<?php

namespace App\Models;


use Carbon\Carbon;
use Illuminate\Database\Eloquent\SoftDeletes;

class InviteOneOnOneStudent extends BaseModels
{
    use SoftDeletes;

    protected $table = 'invite_one_on_one_students';
    protected $primaryKey = 'student_id';

    protected $fillable = [
        'student_id','has_billing','has_consumption','status'
    ];

    const HAS_BILLING_NO = 0;
    const HAS_BILLING_YES = 1;

    const HAS_CONSUMPTION_NO = 0;
    const HAS_CONSUMPTION_YES = 1;

    const STATUS_NO = 0;
    const STATUS_YES = 1;

    public function getStudentId()
    {
        return $this->student_id;
    }

    public function setStudentId($value)
    {
        $this->student_id = $value;
    }

    public function getHasBilling()
    {
        return $this->has_billing;
    }

    public function setHasBilling($value)
    {
        $this->has_billing = $value;
    }

    public function getHasConsumption()
    {
        return $this->has_consumption;
    }

    public function setHasConsumption($value)
    {
        $this->has_consumption = $value;
    }

    public function getStatus()
    {
        return $this->status;
    }

    public function setStatus($value)
    {
        $this->status = $value;
    }

    public static function setInviteStatus($studentId, $hasBilling = 0, $hasConsumption = 0)
    {
        if (!$hasBilling && !$hasConsumption) {
            return ;
        }
        /**
         * @var InviteOneOnOneStudent $inviteOneOnOneStudent
         */
        $inviteOneOnOneStudent = InviteOneOnOneStudent::find($studentId);
        if (empty($inviteOneOnOneStudent)) {
            $inviteOneOnOneStudent = new InviteOneOnOneStudent();
            $inviteOneOnOneStudent->setStudentId($studentId);
        }

        if ($inviteOneOnOneStudent->getStatus() == self::STATUS_NO) {
            if ($hasBilling > 0) {
                $inviteOneOnOneStudent->setHasBilling($hasBilling);
            }
            if ($hasConsumption > 0) {
                $inviteOneOnOneStudent->setHasConsumption($hasConsumption);
            }
            if ($inviteOneOnOneStudent->getHasBilling() == self::HAS_BILLING_YES
                && $inviteOneOnOneStudent->getHasConsumption() == self::HAS_CONSUMPTION_YES) {
                $inviteOneOnOneStudent->setStatus(self::STATUS_YES);
                //创建分享、发event到notifications.api
                InviteOneOnOneShare::createShareByStudentIdAndNotify($studentId);
            }
            $inviteOneOnOneStudent->save();
        } else {
            if ($hasBilling > 0) {
                $inviteOneOnOneStudent->setHasBilling($hasBilling);
            }
            if ($hasConsumption > 0) {
                $inviteOneOnOneStudent->setHasConsumption($hasConsumption);
            }
            $inviteOneOnOneStudent->save();
        }
        return ;
    }
}